package com.baking.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.multipart.MultipartFile;
import org.apache.commons.io.FilenameUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @Author:wrb
 * @Date :2020/1/13/10:07
 * @Describtion:操作文件
 **/
public class FileUtil {

    /** @Description:
     * @Author:wrb
     * @Date:2020/1/15/17:12
     * @Param data 是Base64的图片数据
     * @return java.lang.String
     */
    public static String download(String data){
        String basePath = "E:/upload/";
        //获取图片保存地址
        String fileSavePath = null;
        try {
            String links = data.replaceAll(" ", "+");
            if(links.startsWith("data:image")) {
                String suffix = "jpg"; // 根据links[i]获取后缀名  data:image/jpg；
                String newName = System.currentTimeMillis() + Math.random() * 1000000 + "." + suffix;
                //把".."分割成{"data:image/jpeg;base64,","AKLJF9.."}两部分
                String[] imgData = links.split("base64,",0);
                Base64Img.base64ToImg(imgData[1],basePath+"/" + newName);
                fileSavePath ="upload/"+ newName;
            }else {
                System.out.println("失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileSavePath;
    }

    public static String getBasePath() {
        return ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("/");
    }
}
